package it.rikesh.aip2011.dataloading;

import it.rikesh.aip2011.shared.model.FaunaFlora;
import it.rikesh.aip2011.shared.model.Trail;
import it.rikesh.aip2011.shared.model.TrailFaunaFlora;

import java.io.InputStream;
import java.util.Stack;

import javax.servlet.ServletContext;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.slim3.datastore.Datastore;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;

/**
 * @author  RN
 */
public class FaunaFloraHandler extends DefaultHandler{
    private Stack<FaunaFlora> faunaStack;
    private String characters;
    private String[] trails;
    
    public FaunaFloraHandler(ServletContext context){
        SAXParserFactory factory = SAXParserFactory.newInstance();
        try {
            SAXParser saxParser = factory.newSAXParser();
            InputStream is = context.getResourceAsStream("/data/fauna.xml");
            saxParser.parse(is, this);
        } catch (Throwable t) {

            t.printStackTrace();

        }
    }
    
    public void startDocument() 
    throws SAXException {

        faunaStack = new Stack<FaunaFlora>();

    }

    public void startElement(String namespaceURI, 
            String localName,
            String qualifiedName,
            Attributes attributes) 
    throws SAXException {

        if (qualifiedName.equals("fauna")) {

            FaunaFlora fauna = new FaunaFlora();
            Key key = Datastore.createKey(FaunaFlora.class, Long.parseLong(attributes.getValue("id")));
            fauna.setKey(key);
            faunaStack.push(fauna);

        }

    }

    public void endElement(String namespaceURI, 
            String simpleName,
            String qualifiedName) 
    throws SAXException {

        if (!faunaStack.isEmpty()) {

            if (qualifiedName.equals("fauna")) {
                FaunaFlora fauna=faunaStack.pop();
                Datastore.put(fauna);
                Trail tr;
                for (String trail : trails) {
                    Key key=Datastore.createKey(Trail.class, Long.parseLong(trail));
                    tr = Datastore.get(Trail.class, key);
                    TrailFaunaFlora tf=new TrailFaunaFlora();
                    tf.getTrailRef().setModel(tr);
                    tf.getFaunaFloraRef().setModel(fauna);
                    Datastore.put(tf);
                }
//                Key key=Datastore.createKey(Trail.class, Long.parseLong(trail));
//                Trail tr = Datastore.get(Trail.class, key);
//                fauna.getTrailRef().setModel(tr);
//                Datastore.put(fauna);
            } 
            else if (qualifiedName.equals("name")) {

                FaunaFlora fauna = faunaStack.pop();
                fauna.setName(characters);
                faunaStack.push(fauna);

            } 
            else if (qualifiedName.equals("type")) {

                FaunaFlora fauna = faunaStack.pop();
                fauna.setType(characters);
                faunaStack.push(fauna);

            } 
            else if (qualifiedName.equals("detail")) {

                FaunaFlora fauna = faunaStack.pop();
                fauna.setDetail(new Text(characters));
                faunaStack.push(fauna);

            }
            else if (qualifiedName.equals("img")) {

                FaunaFlora fauna = faunaStack.pop();
                fauna.setImg(characters);
                faunaStack.push(fauna);

            }
            else if (qualifiedName.equals("trail")) {

                trails=characters.split(";");

            }
            
        }

    }

    public void characters(char buf[], int offset, int len) 
    throws SAXException {

        characters = new String(buf, offset, len);

    }
}
